package com.lifq.tmp;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: LiFuqiang
 * @Date: 2023/7/6 16:34
 * @Version 1.0
 * @description
 */
public class MakeSqlInitScript {

    public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
        //findNeedEmptyTable();
        tt();
    }

    public static void tt() throws ClassNotFoundException, SQLException, IOException {
        Set<String> needCleanTables = findNeedEmptyTable();


        String jdbcUrl = "jdbc:mysql://10.0.113.65:33067/sy_dev_0706";
        String user = "bdpass";
        String apwd = "5EPMCdD@JPjO7Qgyd";
        Class.forName("com.mysql.cj.jdbc.Driver");

        Connection c = DriverManager.getConnection(jdbcUrl,user,apwd);

        Statement statement = c.createStatement();

        for(String tbl: needCleanTables){
            String sql = "truncate table sy_dev_0706."+tbl + ";";
            System.out.println(sql);
            try {
                statement.execute(sql);
            }catch (Exception e){
                System.out.println(tbl);
            }
        }

        c.close();
    }

    public static Set<String> findNeedEmptyTable() throws IOException {
        Set<String> allTables = Files.readAllLines(Paths.get("all_tables")).stream().collect(Collectors.toSet());
        Set<String> initTables = Files.readAllLines(Paths.get("init_tables")).stream().collect(Collectors.toSet());

        for(String table: initTables){
            allTables.remove(table);
        }
        System.out.println(allTables.size());
        return allTables;
    }
}
